package com.chart.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartConfigsUser;
import com.chart.chart.domian.req.PUReq;
import com.chart.chart.domian.vo.PUVo;
import com.chart.chart.mapper.ChartConfigsMapper;
import com.chart.chart.mapper.ChartConfigsUserMapper;
import com.chart.chart.service.IChartConfigsService;
import com.chart.chart.service.IChartConfigsUserService;
import com.chart.common.core.domain.entity.ChartClick;
import com.chart.system.mapper.ChartClickMapper;
import com.chart.system.service.IChartClickService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChartClickServiceImpl extends ServiceImpl<ChartClickMapper, ChartClick> implements IChartClickService {


    @Autowired
    private ChartClickMapper chartClickMapper;

    @Autowired
    private ChartConfigsMapper chartConfigsMapper;
    @Autowired
    private IChartConfigsUserService chartConfigsUserService;
    @Autowired
    private IChartConfigsService chartConfigsService;
    @Autowired
    private ChartConfigsUserMapper chartConfigsUserMapper;
    

    @Override
    public List<ChartClick> queryPV() {
        List<ChartClick> chartClicks = chartClickMapper.queryPV();
        return chartClicks;
    }


    /**
     * UV查询
     * @return
     */
    @Override
    public List<ChartClick> queryUV() {
        List<ChartClick> chartClicks = chartClickMapper.queryUV();
        return chartClicks;
    }

    /**
     * PU查询
     * @param req
     * @return
     */
/*
    @Override
    public List<PUVo> queryPU(PUReq req) {
        LambdaQueryWrapper<ChartConfigs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChartConfigs::getStatus, 0);
        List<ChartConfigs> chartConfigs = chartConfigsMapper.selectList(wrapper);
        
        Integer type = req.getType();
        
        if(type==1){
            List<PUVo> puVos = chartClickMapper.queryPU01(req);
            for (PUVo puVo : puVos) {
                String name = puVo.getName();
                String pv = puVo.getPv();
                String uv = puVo.getUv();
            }


            for (ChartConfigs chartConfig : chartConfigs) {
                String configName = chartConfig.getConfigName();

            }

            return  
        }else if(type==2){
            return  chartClickMapper.queryPU02(req);
        }


        return Collections.emptyList();
    }

*/




/*
    @Override
    public List<PUVo> queryPU(PUReq req) {
        // 获取所有有效的 ChartConfigs
        LambdaQueryWrapper<ChartConfigs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChartConfigs::getStatus, 0);
        List<ChartConfigs> chartConfigs = chartConfigsMapper.selectList(wrapper);

        // 获取所有有效的 ChartConfigsUser
        LambdaQueryWrapper<ChartConfigsUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ChartConfigsUser::getStatus, 0);
        List<ChartConfigsUser> chartConfigsUsers = chartConfigsUserMapper.selectList(lqw);

        Integer type = req.getType();
        List<PUVo> puVos = new ArrayList<>();

        if (type == 1) {
            // 处理 type == 1 的逻辑
            puVos = chartClickMapper.queryPU01(req);
            Map<String, PUVo> nameToPUVoMap = new HashMap<>();

            // 将 PUVo 按名称映射
            for (PUVo puVo : puVos) {
                nameToPUVoMap.put(puVo.getName(), puVo);
            }

            // 遍历 chartConfigs 来更新或添加 PUVo
            for (ChartConfigs chartConfig : chartConfigs) {
                Long id = chartConfig.getId();
                String configName = chartConfig.getConfigName();

                if (nameToPUVoMap.containsKey(configName)) {
                    PUVo matchedPUVo = nameToPUVoMap.get(configName);
                    Integer pv = Integer.valueOf(matchedPUVo.getPv());
                    Integer uv = Integer.valueOf(matchedPUVo.getUv());
                    matchedPUVo.setPv(String.valueOf(pv));
                    matchedPUVo.setUv(String.valueOf(uv));
                    matchedPUVo.setConfigId(id);
                } else {
                    // 如果没有匹配的 PUVo，创建新的
                    PUVo newPUVo = new PUVo();
                    newPUVo.setName(configName);
                    newPUVo.setPv("0"); // 设置默认 pv
                    newPUVo.setUv("0"); // 设置默认 uv
                    newPUVo.setConfigId(id);
                    puVos.add(newPUVo);
                }
            }

            return puVos;

        } else if (type == 2) {
            // 处理 type == 2 的逻辑
            puVos = chartClickMapper.queryPU02(req);  // 使用 queryPU02 获取相应数据

            Map<String, PUVo> nameToPUVoMap = new HashMap<>();

            // 将 PUVo 按名称映射
            for (PUVo puVo : puVos) {
                nameToPUVoMap.put(puVo.getName(), puVo);
            }

            // 遍历 chartConfigsUsers 来更新或添加 PUVo
            for (ChartConfigsUser chartConfigsUser : chartConfigsUsers) {
                Long id = chartConfigsUser.getId();
                String configName = chartConfigsUser.getChartName(); // 获取图表名称

                if (nameToPUVoMap.containsKey(configName)) {
                    PUVo matchedPUVo = nameToPUVoMap.get(configName);
                    Integer pv = Integer.valueOf(matchedPUVo.getPv());
                    Integer uv = Integer.valueOf(matchedPUVo.getUv());
                    matchedPUVo.setPv(String.valueOf(pv));
                    matchedPUVo.setUv(String.valueOf(uv));
                    matchedPUVo.setConfigId(id);
                } else {
                    // 如果没有匹配的 PUVo，创建新的
                    PUVo newPUVo = new PUVo();
                    newPUVo.setName(configName);
                    newPUVo.setPv("0"); // 设置默认 pv
                    newPUVo.setUv("0"); // 设置默认 uv
                    newPUVo.setConfigId(id);
                    puVos.add(newPUVo);
                }
            }

            return puVos;
        }

        return Collections.emptyList();
    }*/


    @Override
    public List<PUVo> queryPU(PUReq req) {
        Integer type = req.getType();

        if (type == 1) {

      return chartClickMapper.queryPU01(req);

        } else if (type == 2) {
            // 处理 type == 2 的逻辑
            return chartClickMapper.queryPU02(req);  // 使用 queryPU02 获取相应数据
        }

        return Collections.emptyList();
    }






}
